package com.springboot.service.impl;

import com.springboot.mapper.CreditMapper;
import com.springboot.mapper.UserMapper;
import com.springboot.model.po.*;
import com.springboot.service.CreditService;
import util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class CreditServiceImpl implements CreditService {
    @Autowired
    private CreditMapper creditMapper;
    @Autowired
	private UserMapper userMapper;

    @Override
    public BigDecimal getTotalCreditByUserId(String userId) {
        List<Credit> list = creditMapper.getTotalCreditByUserId(userId);
        CreditList creditList = new CreditList(list);
        BigDecimal totalCredit = creditList.getTotalNum();
        for (Credit credit : creditList.getExpiredList())
        	//删除过期的积分
            creditMapper.deleteCreditCopyByCreditId(credit.getCreditId());
        //更新用户的积分
        
        return totalCredit;
    }

    @Override
    public List<Credit> getAvailableCreditByUserId(String userId) {
        List<Credit> list = creditMapper.getTotalCreditByUserId(userId);
        List<Credit> returnlist = new ArrayList<>();
        //将list中所有expierdTime小于当前时间的积分删除
        for (Credit credit : list) {
            if (CommonUtil.compareDate(credit.getExpiredTime(), CommonUtil.getCurrentDate1()) == 1) {
                returnlist.add(credit);
            } else {
                creditMapper.deleteCreditCopyByCreditId(credit.getCreditId());
            }
        }
        return returnlist;
    }

    @Override
    public List<Credit> getCreditByUserId(String userId) {
        return creditMapper.getCreditByUserId(userId);
    }

    @Override
    public List<Credit> getCreditByUserIdAndChannelType(String userId, int channelType) {
        return creditMapper.getCreditByUserIdAndChannelType(userId, channelType);
    }

    @Override
    @Transactional
    public int insertCredit(Credit credit) {
        credit.setCreateTime(CommonUtil.getCurrentDate1());
        if (credit.getNum().compareTo(BigDecimal.ZERO) >= 0) {
        	//更新累计积分
        	BigDecimal oldTotal = userMapper.getMaxPoints(credit.getUserId());
        	BigDecimal newTotal = oldTotal.add(credit.getNum());
        	userMapper.updateMaxPoints(credit.getUserId(), newTotal);
            credit.setExpiredTime(CommonUtil.getCurrentDate2());
            creditMapper.insertCreditCopy(credit);
        } else {
            BigDecimal left = credit.getNum().negate();
            List<Credit> list = creditMapper.getTotalCreditByUserId(credit.getUserId());
            CreditList creditList = new CreditList(list);
            //扣分
            Credit UpdateCredit = creditList.getUpdatedCredit(left);
            creditMapper.updateCreditCopyByCreditId(UpdateCredit.getCreditId(), UpdateCredit.getNum());
            for (Credit credit2 : creditList.getDeletedList()) {
            	//更新标记位
                creditMapper.deleteCreditCopyByCreditId(credit2.getCreditId());
            }
        }
        return creditMapper.insertCredit(credit);
    }
}
